<?php

namespace app\oa\controller;

use app\oa\model\Mission;
use app\oa\model\Report;
use app\oa\model\User;
use think\Loader;
use think\Request;

class MissionController extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $mobile = getSession('mobile');
        $data = User::getUserInfoByMobile($mobile);
        if (!empty($data)) {
            $missionList = Mission::getMissionList($data['uid']);
//            dump($missionList);

            $count = Mission::getMissionListCount($data['uid']);
            $this->assign('uid', $data['uid']);
            $this->assign('lists', $missionList);
            $this->assign('data', $data);
            $this->assign('count', $count);

        } else {
            $this->redirect('/login');
        }
        return $this->fetch('mission/index');
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description: 下拉分页获取数据
     * @time: 2017年7月24日 13:16:013
     * @return array
     */
    public function ajaxGetList()
    {

        $mobile = getSession('mobile');
        $data = User::getUserInfoByMobile($mobile);
        if (Request::instance()->isAjax()) {
            $start = Request::instance()->param('start', 10, 'intval');
            $missionList = Mission::getMissionList($data['uid'], $start);
//            dump($missionList);
            $this->assign('lists', $missionList);
            $this->assign('data', $data);

            if (!empty($missionList[0])) {
                return self::showReturnCodeWithOutData(1, '请求结果...', $missionList);
            } else {
                return self::showReturnCodeWithOutData(-1, '老铁.真的没有了...');
            }

        }

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

        $data = User::getPartPerson();

        $this->assign('list', array_recursion($data));
        return $this->fetch('mission/create');
    }


    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {

        $mobile = getSession('mobile');
        if (Request::instance()->isPost()) {
            $postData = $request->post();
            $data = User::getUserInfoByMobile($mobile);
            $res = User::refactorObj($data);
            $saveData = [
                'department_id' => $res->departmentObj['level'],
                'duty_id' => $data['duty_id'],
                'duty_id_level' => $res->duty_level,
                'title' => $postData['title'],
                'mission_comment' => $postData['mission_comment'],
                'leader_id' => $postData['leader_id'],
                'part_res' => $postData['part_res'],
                'over_time' => strtotime($postData['over_time'])
            ];
            $res->missions()->save($saveData);
            if ($res) {
                return self::showReturnCodeWithOutData(1, '提交成功');
            } else {
                return self::showReturnCodeWithOutData(-1, '提交失败');
            }
        }
        //
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description: 领取任务
     * @time:2017年7月13日 11:41:20
     * @return array
     */
    public function getMission()
    {
        if (Request::instance()->isAjax()) {
            $mobile = getSession('mobile');
            $data = User::getUserInfoByMobile($mobile);
            $mission_id = Request::instance()->param('id', 10, 'intval');
            $res = Mission::getReceiveUidByMissionId($mission_id, 'leader_id,is_get');
            $leader_id = $res->leader_id;
            if ($leader_id != $data['uid']) {
                return self::showReturnCodeWithOutData(-1, '您不能领取该任务');
            }
            $receive_uid = $res->is_get;
            $str = '';
            if ($receive_uid == 0) {
                $str .= $data['uid'];
            } else {
                if (strpos($receive_uid, (string)$data['uid']) !== false) {
                    return self::showReturnCodeWithOutData(-1, '已经领取成功');
                }
                $str .= $receive_uid . ',' . $data['uid'];
            }
            $modify_res = Mission::modifyReceiveField($mission_id, $str);
            if ($modify_res) {
                return self::showReturnCodeWithOutData(1, '领取成功');
            } else {
                return self::showReturnCodeWithOutData(-1, '领取失败');
            }
        }
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description: 完成任务
     * @time: 2017-7-13 16:22:00
     * @param Request $request
     * @return array
     */
    public function completeMission(Request $request)
    {
        if ($request->isAjax()) {
            $id = $request->get('id', '', 'intval');
            $modify_res = Mission::modifyMissionCompleteStatus($id);
            if ($modify_res) {
                return self::showReturnCodeWithOutData(1, '完成任务成功');
            } else {
                return self::showReturnCodeWithOutData(-1, '完成任务失败');
            }
        }
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description: 修改完成率
     * @time: 2017年7月24日 13:50:17
     * @param Request $request
     * @return array
     */
    public function updateRate(Request $request)
    {
        $data = $request->post();
        $res = Mission::update($data);
        if ($res) {
            return self::showReturnCodeWithOutData(1, '提交成功');
        }else{
            return self::showReturnCodeWithOutData(-1, '提交失败');
        }
    }


    public function search(Request $request)
    {
        if ($request->isAjax()) {

            $title = $request->get('search');
            $mobile = getSession('mobile');
            $data = User::getUserInfoByMobile($mobile);
            $data=Mission::search($data['uid'],$title);
            return json($data);
//            $res = Loader::model('User')->getUidBySearch($title);
//            if (!empty($res)) {
//                $mobile = getSession('mobile');
//                $user_data = User::getUserInfoByMobile($mobile);
//                $reportData = Loader::model('Report')->getSearchInfoByUid($user_data, 0);
//            } else {
//                return self::showReturnCodeWithOutData(-1, '数据为空');
//            }
        }
    }
    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
        $data = User::getPartPerson();

        dump(array_recursion($data));
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
